package com.xszx.controller;

import com.github.pagehelper.PageInfo;
import com.xszx.config.PageRequest;
import com.xszx.entity.Client;
import com.xszx.service.ClientService;
import com.xszx.vo.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/client")
public class ClientController {
    @Autowired
    private ClientService clientService;

    @RequestMapping("/clientList")
    public AjaxJson clientList( PageRequest pageRequest,
                                @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer size){
        pageRequest.setPage(page);
        pageRequest.setSize(size);
        AjaxJson ajaxJson = new AjaxJson();
        try {
            PageInfo<Client> clientList = clientService.getClientListInfo(pageRequest);
            ajaxJson.put("clientList",clientList);
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("获取客户列表失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }
    //点击查看每个客户对应的货物信息
    @RequestMapping("/getGoodsList")
    public AjaxJson getClientGoods(@RequestParam("id")Integer id){
        System.out.println("id："+id);
        AjaxJson ajaxJson = new AjaxJson();
        try {
            List<Client> clientGoods = clientService.getClientGoodsById(id);
            ajaxJson.put("clientGoods",clientGoods);
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("获取客户货物信息失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }
    @RequestMapping("/addClient")
    public AjaxJson addClient(@RequestBody Client client){
        System.out.println("client添加列表："+client);
        AjaxJson ajaxJson = new AjaxJson();
        try {
            clientService.addClient(client);
            ajaxJson.setErrMsg("添加k客户成功");
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("添加客户失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }


    @RequestMapping("/updateClient")
    public AjaxJson updateClient(@RequestBody Client client){
        AjaxJson ajaxJson = new AjaxJson();
        try {
            clientService.updateClient(client);
            ajaxJson.setErrMsg("修改客户成功");
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("修改客户失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }
    @RequestMapping("/deleteClient")
    public AjaxJson deleteClient(Integer id){
        System.out.println(id);
        AjaxJson ajaxJson = new AjaxJson();
        try {
            clientService.deleteClient(id);
            ajaxJson.setErrMsg("删除客户成功");
        }  catch (Exception e) {
            ajaxJson.setSuccess("false");
            ajaxJson.setErrMsg("删除客户失败");
            e.printStackTrace();
        }
        return ajaxJson;
    }
}
